/**
 * 
 */
package com.platform.common.utils;

import java.util.Collection;
import java.util.Iterator;

/**
 * <pre>
 * 功能：参数校验
 * </pre>
 * 
 * @author wei.gao
 */
public final class Assert {
	private Assert() {
	}
	
	/**
	 * <pre>
	 * 功能：判断是否为空，先判断是否为null，支持String,Collection,Iterator,Iterable类型
	 * </pre>
	 * 
	 * @param obj
	 * @param message
	 */
	public static void notEmpty(String name, Object obj) {
		if (obj == null) {
			throw _adjustStacktrace(new IllegalArgumentException("[" + name + "] must not be empty"));
		}
		if (obj instanceof String) {
			if (ValidateUtils.isBlank((String) obj)) {
				throw _adjustStacktrace(new IllegalArgumentException("[" + name + "] must not be empty"));
			}
		}
		if (obj instanceof Collection) {
			if (((Collection<?>) obj).isEmpty()) {
				throw _adjustStacktrace(new IllegalArgumentException("[" + name + "] must not be empty"));
			}
		}
		if (obj instanceof Iterator) {
			if (!((Iterator<?>) obj).hasNext()) {
				throw _adjustStacktrace(new IllegalArgumentException("[" + name + "] must not be empty"));
			}
		}
		if (obj instanceof Iterable) {
			if (!((Iterable<?>) obj).iterator().hasNext()) {
				throw _adjustStacktrace(new IllegalArgumentException("[" + name + "] must not be empty"));
			}
		}
	}
	
	private static RuntimeException _adjustStacktrace(final RuntimeException ex) {
		final StackTraceElement[] stack = ex.getStackTrace();
		final StackTraceElement[] newStack = new StackTraceElement[stack.length - 1];
		System.arraycopy(stack, 1, newStack, 0, stack.length - 1);
		ex.setStackTrace(newStack);
		return ex;
	}
}
